package it.blog.controller;

import it.blog.stub.*;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class PortalServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		boolean loggato=false;
		boolean notBlog=false;

		BlogPortal bp = new BlogPortalService().getPort(BlogPortal.class);
		HttpSession session = request.getSession();
		
		if(session == null){
			return;
		}
		
		User user = (User) session.getAttribute("user");
		
		List<Blog> blogList = bp.getBlogList();
		List<BlogCategory> blogCategoryList = bp.getBlogCategoryList();
		
		if( session!=null && user!=null ){
			loggato=true;
			if(user.getBlogURL() == null || user.getBlogURL().equals("") ){
				notBlog=true;
			}
		} 
		session.removeAttribute("mess");
		session.setAttribute("blogList",blogList);
		session.setAttribute("blogCategoryList",blogCategoryList);

		session.setAttribute("loggato", loggato);
		session.setAttribute("notBlog", notBlog);
		
		
		response.sendRedirect(response.encodeRedirectURL(request.getContextPath()+"/home.jsp?kop=home"));

		
	}
	


}
